package com.fz.util;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * Created by Z-C-Y on 2018-03-04.
 * 用来实现验证码图片的生成
 */
@WebServlet("/check.do")
public class Check extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //先进行处理乱码
        req.setCharacterEncoding("utf-8");
        //表示向传输的数据是一个图片
        resp.setContentType("image/jpeg");
        //产生随机数据
        Random random = new Random();
        //设置验证码的长宽
        int width = 120;
        int height  = 50;
        //使用图片处理方法
        BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //使用绘图方法
        Graphics g = bufferedImage.getGraphics();
        //设置验证码的背景颜色
        Color white = new Color(255,255,255);
        g.setColor(white);
        //绘制矩形
        g.fillRect(0,0,width,height);
        //设置验证码现实的内容
        String str = "ABCDEFGHIJKLMNPQRSTUVWXYabcdefghijklmnpqrstuvwxy23456789";
        StringBuilder stringBuilder = new StringBuilder();
        //使用StringBufilder把数据进行拼接，，，---进行遍历
        for(int i = 0; i<4;i++){
            //设置验证码的背景随机颜色
            g.setColor(new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
            //设置验证码的字体的大小
            g.setFont(new Font("黑体",Font.BOLD,random.nextInt(35)+18));
            char c = str.charAt(random.nextInt(str.length()));
            stringBuilder.append(c);
            int x = i*25+5;
            int y = random.nextInt(30)+20;
            g.translate(0,0);
            int m = random.nextInt(20)-10;
            g.drawString(String.valueOf(c),x,y);
           // System.out.println(c);

        }
        //把验证传递给页面
        req.getSession().setAttribute("checkstr", stringBuilder.toString());
        //输出到界面
        ImageIO.write(bufferedImage, "jpg", resp.getOutputStream());






        	     /*    //添加干扰
			for (int n = 0; n < 2; n++) {
				//设定字体的颜色-》产生随机的颜色
				//Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),100)最后一个值是透明度
				g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255), 100));
				//验证码的的字体
				g.setFont(new Font("黑体", Font.BOLD, r.nextInt(5) + 10));
				//得到str中的随机的字母，
				char s = str.charAt(r.nextInt(str.length()));
				int x = r.nextInt(width);
				int y = r.nextInt(height);//设置字母的大小
				//坐标/String.valueOf(s)方法是讲s转换成String字符串
				g.drawString(String.valueOf(s), x, y);

			}*/
/*        //添加干扰圆形
        for (int n = 0; n < 2; n++) {
            //设定字体的颜色-》产生随机的颜色
            //Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),100)最后一个值是透明度
            g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255), 100));
            int x = random.nextInt(width);
            int y = random.nextInt(height);//设置字母的大小
            int ww = random.nextInt(30) + 10;
            int hh = ww;
            //r.nextBoolean()方法，随机真假
            //让验证码随机产生矩形与椭圆形
            if (random.nextBoolean()) {
                //矩形
                g.fillRect(x, y, ww, ww);
            } else {
                //添加椭圆形
                g.fillOval(x, y, ww, hh);
            }

        }*/

		/*	//添加干扰线、
			for (int n = 0; n < 10; n++) {
				//设定字体的颜色-》产生随机的颜色
				//Color(r.nextInt(255),r.nextInt(255),r.nextInt(255),100)最后一个值是透明度
				//更改一下线条的粗细
				g.setStroke((new BasicStroke(3f)));
				g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255), 100));
				int x1 = r.nextInt(width);
				int y1 = r.nextInt(height);
				int x2 = r.nextInt(width);
				int y2 = r.nextInt(height);
				//添加椭圆形
				g.drawLine(x1, y1, x2, y2);

			}
*/

    }
}
